package top.aicey.ai.sparring.llm.impl.client;

import com.alibaba.dashscope.app.Application;
import com.alibaba.dashscope.app.ApplicationParam;
import com.alibaba.dashscope.app.ApplicationResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import top.aicey.ai.sparring.llm.ILLMClientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 百炼客户端
 */
@Component("bailianClientService")
public class BailianClientService implements ILLMClientService {

    private static final Logger log = LoggerFactory.getLogger(BailianClientService.class);
    @Autowired
    private Application bailianApplication;

    @Value("${llm.evaluateScoreInfo.bailian.evaluate.appId:e451f07490674a45ac9582d689994b6e}")
    private String evaluateAppId;


    @Value("${llm.evaluateScoreInfo.bailian.dimensions.appId:66a64db9084c410480410579b1cd5f91}")
    private String dimensionsAppId;

    @Value("${llm.evaluateScoreInfo.bailian.apiKey:sk-217374ae86bf46bda37d12c018d96f7a}")
    private String apiKey;



    @Override
    public String query(String query, Map<String, Object> params) {

        String bizParams =
                JSON.toJSONString(params);
        ApplicationParam param = ApplicationParam.builder()
                .apiKey(apiKey)
                .appId(evaluateAppId)
                .prompt(query)
                .bizParams(JsonUtils.parse(bizParams))
                .build();

        try {
            ApplicationResult result = bailianApplication.call(param);
            return result.getOutput().getText();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            log.info("query bailian llm error",e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public String generateDimensions(String query, Integer count) {
        Map<String,Object> params = Maps.newHashMap();

        params.put("dimension_num",count);

        String bizParams =
                JSON.toJSONString(params);
        ApplicationParam param = ApplicationParam.builder()
                .apiKey(apiKey)
                .appId(dimensionsAppId)
                .prompt(query)
                .bizParams(JsonUtils.parse(bizParams))
                .build();

        try {
            ApplicationResult result = bailianApplication.call(param);
            return result.getOutput().getText();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            log.info("generateDimensions bailian llm error",e);
            throw new RuntimeException(e);
        }
    }
}
